﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class RoomTypeDAL
    {
        public RoomTypeDAL(HoteContent hoteContent)
        {
            HoteContent = hoteContent;
        }

        public HoteContent HoteContent { get; }
        /// <summary>
        /// 房型添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int RoomTypeAdd(RoomTypeModel info)
        {
            try
            {
                var res = HoteContent.roomTypeModels.Count(o => o.RoomTypeName == info.RoomTypeName);
                if (res > 0)
                {
                    return -1;//用户名已存在
                }
                HoteContent.roomTypeModels.Add(info);
                return HoteContent.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="totalCount">页容量</param>
        /// <param name="totalPage">页码</param>
        /// <param name="Pageindex">当前页</param>
        /// <param name="PageSize">页码</param>
        /// <param name="RoomTypeName">房型名称</param>
        /// <param name="Asay">房型大小</param>
        /// <returns></returns>
        public List<RoomTypeModel> GetRoomTypesList(out int totalCount,out int totalPage,int Pageindex=1,int PageSize=2,string? RoomTypeName="",int? max=null,int?min=null)
        {
            try
            {
                var list = HoteContent.roomTypeModels.Where(o => o.IsDelete == false).ToList();
                if (!string.IsNullOrEmpty(RoomTypeName))
                {
                    list=list.Where(o=>o.RoomTypeName.Contains(RoomTypeName)).ToList();
                }
                if (max!=null)
                {
                    list = list.Where(o => o.Asay<max).ToList();
                }
                if (min != null)
                {
                    list = list.Where(o => o.Asay > min).ToList();
                }
                totalCount = list.Count();
                totalPage = (int)Math.Ceiling(totalCount * 1.0 / PageSize);
                list=list.OrderByDescending(o=>o.Id).Skip((Pageindex-1)* PageSize).Take(PageSize).ToList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
